package net.malkuth.test;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.provider.MediaStore;
import android.text.format.DateFormat;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager.LayoutParams;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.Toast;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Calendar;
import java.util.Locale;

/**
 * 开始上课
 *
 * @author X_zH0u
 */
public class BeginClassActivity extends BaseNewActivity {

    private Dialog dialog;
    private Button btn_alone, btn_help1, btn_help2, btn_help3;
    private View layouut_alone;
    private int left, top, right, bottom;
    private LinearLayout ll_help1, ll_help2, ll_help3, ll_help_alone;

    /**
     * 初始化坐标按钮位置
     */
    int[] int_alone = new int[4];


    /**
     * 单击事件
     */
    public void click(View view) {
        switch (view.getId()) {
            // 切换题包
            case R.id.btn_switchpackage:
                dialog = new AlertDialog.Builder(this).create();
                dialog.show();
                dialog.setCanceledOnTouchOutside(false);
                View v = LayoutInflater.from(this).inflate(R.layout.activity_dialog_questions, null);
                Window window = dialog.getWindow();
                window.setContentView(v);
                LayoutParams layoutParams = window.getAttributes();
                layoutParams.height = LayoutParams.WRAP_CONTENT;
                layoutParams.width = LayoutParams.WRAP_CONTENT;
                window.setAttributes(layoutParams);
                break;
            // 结束课程
            case R.id.btn_endclass:
                startActivity(new Intent(this, ReturnsActivity.class));
                this.finish();
                break;
            // 切换
        /*
         * case R.id.btn_change: dialog.dismiss(); break;
		 */
            // 关闭
            case R.id.iv_dialog_close:
                dialog.dismiss();
                break;
            // 拍照
            case R.id.iv_photo:
                Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                startActivityForResult(intent, 1);
                break;
            //辅助一
            case R.id.btn_help1:
                ll_help1.setVisibility(View.VISIBLE);
                ll_help2.setVisibility(View.GONE);
                ll_help3.setVisibility(View.GONE);
                ll_help_alone.setVisibility(View.GONE);
                break;
            //辅助二
            case R.id.btn_help2:
                ll_help2.setVisibility(View.VISIBLE);
                ll_help1.setVisibility(View.GONE);
                ll_help3.setVisibility(View.GONE);
                ll_help_alone.setVisibility(View.GONE);
                break;
            //辅助三
            case R.id.btn_help3:
                ll_help3.setVisibility(View.VISIBLE);
                ll_help1.setVisibility(View.GONE);
                ll_help2.setVisibility(View.GONE);
                ll_help_alone.setVisibility(View.GONE);
                break;
            //独立
            case R.id.btn_alone:
                ll_help_alone.setVisibility(View.VISIBLE);
                ll_help1.setVisibility(View.GONE);
                ll_help2.setVisibility(View.GONE);
                ll_help3.setVisibility(View.GONE);
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == Activity.RESULT_OK) {
            String sdStatus = Environment.getExternalStorageState();
            if (!sdStatus.equals(Environment.MEDIA_MOUNTED)) {
                Toast.makeText(this, "SD卡不可用", Toast.LENGTH_SHORT);
                return;
            }
            String name = new DateFormat().format("yyyyMMdd_hhmmss", Calendar.getInstance(Locale.CHINA)) + ".jpg";
            Toast.makeText(this, name, Toast.LENGTH_LONG).show();
            Bundle bundle = data.getExtras();
            // 获取相机返回的数据，并转换为Bitmap图片格式
            Bitmap bitmap = (Bitmap) bundle.get("data");
            FileOutputStream fos = null;
            File file = new File("/sdcard/myImage/");
            // 创建文件夹
            file.mkdirs();
            String fileName = "/sdcard/myImage/" + name;
            try {
                fos = new FileOutputStream(fileName);
                // 把数据写入文件
                bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } finally {
                try {
                    fos.flush();
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            // 发广播把图片保存的手机图库
            Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
            File file2 = new File(fileName);
            Uri uri = Uri.fromFile(file2);
            intent.setData(uri);
            BeginClassActivity.this.sendBroadcast(intent);
        }
    }

    @Override
    public void setupView() {
        ll_help1 = (LinearLayout) findViewById(R.id.ll_help1);
        ll_help2 = (LinearLayout) findViewById(R.id.ll_help2);
        ll_help3 = (LinearLayout) findViewById(R.id.ll_help3);
        ll_help_alone = (LinearLayout) findViewById(R.id.ll_help_alone);
        btn_alone = (Button) findViewById(R.id.btn_alone);
        btn_help1 = (Button) findViewById(R.id.btn_help1);
        btn_help2 = (Button) findViewById(R.id.btn_help1);
        btn_help3 = (Button) findViewById(R.id.btn_help1);
        btn_alone.setOnTouchListener(touch);
        layouut_alone = findViewById(R.id.layouut_alone);

        ll_help_alone.setVisibility(View.VISIBLE);
    }

    @Override
    public void setContent() {
        setContentView(R.layout.activity_class_begins);
    }

    @Override
    public void setModel() {

    }

    @Override
    public boolean getIntentValue() {
        return true;
    }


    /**
     * 独立的Touch事件
     */
    View.OnTouchListener touch = new View.OnTouchListener() {
        int lastX, lastY;

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            // TODO Auto-generated method stub
            int ea = event.getAction();
            switch (ea) {
                case MotionEvent.ACTION_DOWN:
                    lastX = (int) event.getRawX();// 获取触摸事件触摸位置的原始X坐标
                    lastY = (int) event.getRawY();
                    if (int_alone[1] == int_alone[2]) {
                        Log.i("touch", "赋值1" + int_alone[1]);
                        Log.i("touch", "赋值" + int_alone[2]);
                        InitializationXY();
                    }
                    left = v.getLeft();
                    top = v.getTop();
                    right = v.getRight();
                    bottom = v.getBottom();
                    break;
                case MotionEvent.ACTION_MOVE:
                    int dx = (int) event.getRawX() - lastX;
                    int dy = (int) event.getRawY() - lastY;
                    int l = v.getLeft() + dx;
                    int b = v.getBottom() + dy;
                    int r = v.getRight() + dx;
                    int t = v.getTop() + dy;
                    // 下面判断移动是否超出屏幕
                    if (l < 0) {

                        l = 0;
                        r = l + v.getWidth();
                    }
                    if (t < 0) {
                        t = 0;
                        b = t + v.getHeight();
                    }
                    if (r > layouut_alone.getWidth()) {
                        r = layouut_alone.getWidth();
                        l = r - v.getWidth();
                    }
                    if (b > layouut_alone.getHeight()) {
                        b = layouut_alone.getHeight();
                        t = b - v.getHeight();
                    }
                    v.layout(l, t, r, b);
                    lastX = (int) event.getRawX();
                    lastY = (int) event.getRawY();
                    v.postInvalidate();
                    break;
                case MotionEvent.ACTION_UP:
                    int top, bottom;
                    top = v.getTop();
                    bottom = v.getBottom();
                    if (int_alone[0] < top && int_alone[1] < bottom && bottom >= 400) {
                        Toast.makeText(BeginClassActivity.this, "错误", Toast.LENGTH_SHORT).show();
                    } else if (int_alone[0] > top && int_alone[1] > bottom && top <= 10) {
                        Toast.makeText(BeginClassActivity.this, "正确", Toast.LENGTH_SHORT).show();
                    }

                    startTask(v);
                    break;
            }
            return false;
        }
    };
    Handler myhandler = new Handler();

    private void startTask(final View v) {
        myhandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                v.layout(left, top, right, bottom);//复位
                v.postInvalidate();
            }
        }, 200);
    }

    private void InitializationXY() {
        int_alone[0] = btn_alone.getTop();
        int_alone[1] = btn_alone.getBottom();
        int_alone[2] = btn_alone.getLeft();
        int_alone[3] = btn_alone.getRight();
        Log.i("touch", "初始化值bottom" + int_alone[1]);
        Log.i("touch", "初始化值left" + int_alone[2]);
    }
}
